package jwdt.xadmin.finace.action;

import jwdt.xadmin.finace.bean.HaUser;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller("/xadmin/finace")
public class UserAction extends FinaceAction {

	@RequestMapping("/userlist")
	public Page doUserlist(Form f, Module m) throws Exception {
		// 
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", 20);
		
		String sql = " order by px";

		String name = f.getString("name2");
		if (!"".equals(name)) {
			sql = " and name like '%" + name + "%'" + sql;
		}
		int zt = f.getInt("zt2", -1);
		if(zt != -1) {
			sql = " zt=" + f.getInt("zt2", 1) + sql;
		} else {
			sql = " 1=1" + sql;
		}		
		//
		PageUtil pu = new PageUtil(HaUser.class, sql, page, pageSize);
		f.add("pager", pu.setUrl(m.getFullPath()+".do"));
		return m.defaultPage();
	}

	@RequestMapping("/useradd")
	public Page doUseradd(Form f, Module m) throws Exception {
		return m.findPage(super.url + "useradd");
	}	
	
	@RequestMapping("/useraddact")
	public Page doUseraddact(Form f, Module m) throws Exception {
		// 
		HaUser obj = (HaUser)f.copy2Class(HaUser.class);
		String ret = "添加成功";
		try {
			Jdb.get().add(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "添加出错";
		}
		f.add("ret", ret);
		return doUseradd(f, m);
	}

	@RequestMapping("/useredit")
	public Page doUseredit(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaUser obj = (HaUser)Jdb.get().get(HaUser.class, id);
		if(obj == null) {
			f.add("ret", "您要修改记录的不存在：id=" + id);
			return m.findPage("tips");
		}
		f.add("obj", obj);
		return m.defaultPage();
	}

	@RequestMapping("/usereditact")
	public Page doUsereditact(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaUser obj = (HaUser)Jdb.get().get(HaUser.class, id);
		if(obj == null) {
			f.add("ret", "您要修改记录的不存在：id=" + id);
			return m.findPage("tips");
		}
		obj = (HaUser)f.copy2Obj(obj);
		String ret = "修改成功";
		try {
			Jdb.get().update(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "修改出错";
		}
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "userlist.do");
	}

	@RequestMapping("/userdel")
	public Page doUserdel(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaUser obj = (HaUser)Jdb.get().get(HaUser.class, id);
		if(obj == null) {
			f.add("ret", "您要删除记录的不存在：id=" + id);
			return m.findPage("tips");
		}
		String ret = "删除成功";
		try {
			Jdb.get().del(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "删除出错";
		}
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "userlist.do");
	}

}
